package com.txy.webmanagement.control;

import com.txy.webmanagement.dto.SysCompanyDTO;
import com.txy.webmanagement.model.SysCompany;
import com.txy.webmanagement.service.SysCompanyService;
import com.txy.webmanagement.utils.RequestDataUtils;
import com.txy.webmanagement.utils.StringUtils;
import com.txy.webmanagement.utils.SysParrentUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 销售方管理
 *
 * @author wf
 * @date 2019-01-01$
 **/
@Controller
@RequestMapping(value = "company")
public class CompanyControl {
    @Autowired
    private SysCompanyService sysCompanyService;

    @RequestMapping(value = "companyList")
    public String companyList(SysCompany company, Model m) {

        return "company/companylist";
    }

    @RequestMapping(value = "ajaxCompany")
    @ResponseBody
    public RequestDataUtils ajaxCompany(SysCompanyDTO dto) {
        RequestDataUtils req = new RequestDataUtils();
        List<SysCompany> li = sysCompanyService.pageList(dto);
        req.setData(li);
        return req;
    }

    @RequestMapping(value = "gotoAdd")
    public String gotoAdd(String companyId, String pageType, Model m) {
        SysCompany company = new SysCompany();
        if (StringUtils.checkStringNotNull(companyId)) {
            SysCompany sysCompany = sysCompanyService.getOne(companyId);
            if (sysCompany != null) {
                company = sysCompany;
            }
        }
        m.addAttribute("sysCompany", company);
        m.addAttribute("pageType", pageType);
        return "company/add";
    }

    @RequestMapping(value = "add")
    @ResponseBody
    public RequestDataUtils add(SysCompanyDTO dto) {
        RequestDataUtils req = new RequestDataUtils();
        if (StringUtils.checkStringNotNull(dto.getUuid())) {
            Boolean boo = sysCompanyService.update(dto);
            if (boo) {
                req.setCode(SysParrentUtils.successCode);
                req.setMessage(SysParrentUtils.successMsg);
            } else {
                req.setCode(SysParrentUtils.errorCode);
                req.setMessage(SysParrentUtils.addErrorMsg);
            }

        } else {
            Boolean boo = sysCompanyService.add(dto);
            if (boo) {
                req.setCode(SysParrentUtils.successCode);
                req.setMessage(SysParrentUtils.successMsg);
            } else {
                req.setCode(SysParrentUtils.errorCode);
                req.setMessage(SysParrentUtils.addErrorMsg);
            }
        }
        return req;
    }

    /**
     * 单位详情
     * @param companyId
     * @param m
     * @return
     */
    @RequestMapping(value = "companyInfo")
    public String companyInfo(String companyId, Model m) {
        SysCompany company = new SysCompany();
        if (StringUtils.checkStringNotNull(companyId)) {
            SysCompany sysCompany = sysCompanyService.getOne(companyId);
            if (sysCompany != null) {
                company = sysCompany;
            }
        }
        m.addAttribute("sysCompany", company);
        return "company/companyInfo";
    }

    /**
     * 删除
     * @param dto
     * @return
     */
    @RequestMapping(value = "delete")
    @ResponseBody
    public RequestDataUtils delete(SysCompanyDTO dto) {
        RequestDataUtils req = new RequestDataUtils();
        Boolean boo = sysCompanyService.delete(dto);
        if (boo) {
            req.setCode(SysParrentUtils.successCode);
            req.setMessage(SysParrentUtils.successMsg);
        } else {
            req.setCode(SysParrentUtils.errorCode);
            req.setMessage(SysParrentUtils.deleteErroeMsg);
        }
        return req;
    }

}
